package com.infowarelab.config.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jradweb.core.controller.JMultiActionController;
import org.jradweb.core.orm.PageResult;
import org.jradweb.core.utils.BeanUtils;
import org.jradweb.core.web.FormBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import com.infowarelab.config.model.Service;
import com.infowarelab.config.service.ServiceService;

/**
 * ServiceController class .
 * 
 * Created on Mon Aug 18 11:21:34 CST 2008
 * 
 * @author Code Generator
 * @version $Id: ServiceController.java 157 2008-10-17 10:05:54Z wagzhi $
 */
@Controller
public class ServiceController extends JMultiActionController {
	private static final Log log = LogFactory.getLog(ServiceController.class);

	private ServiceService serviceService;

	public void setServiceService(ServiceService serviceService) {
		this.serviceService = serviceService;
	}

	public ServiceService getServiceService() {
		return this.serviceService;
	}

	public Map<String, Object> index(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return list(request, response);
	}

	public Map<String, Object> list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String strPage = request.getParameter("page");
		Integer page = strPage == null ? 1 : Integer.parseInt(strPage);
		PageResult<Service> pageResult = this.serviceService.getPageResult(
				page, this.getPageSize(), " order by Service.id desc", null);
		map.put("pageResult", pageResult);
		return map;
	}

	public Map<String, Object> show(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");
		Service service = this.getServiceService().getService(
				Integer.parseInt(id));
		map.put("service", service);
		return map;
	}

	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		FormBean<Service> bean = this.webDataBinder
				.bind(new Service(), request);

		if (bean.getHasError()) {
			this.getFlash(request).put("serviceBean", bean);
			this.getFlash(request)
					.put("failureMessage", "message.saved.failed");
			return this.render("js_save_failure", map);
		}

		this.serviceService.save(bean.getBean());
		this.getFlash(request).put("successMessage", "message.saved.success");
		map.put("id", bean.getBean().getId());

		return this.render("js_save_success", map);
	}

	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");

		Service service = this.getServiceService().getService(
				Integer.parseInt(id));

		this.serviceService.delete(service);
		this.getFlash(request).put("message", "message.deleted.success");
		map.put("deletedId", id);
		return this.render("js_delete_success", map);
	}

	public ModelAndView delete_all(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String[] ids = request.getParameterValues("ids");
		Map rm = request.getParameterMap();
		for (String id : ids) {
			Service service = this.getServiceService().getService(
					Integer.parseInt(id));
			this.serviceService.delete(service);
		}
		this.getFlash(request).put("message", "message.deleted.success");
		return this.render("js_delete_success", map);
	}

	public Map<String, Object> create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String failMessage = (String) this.getFlash(request).get("failMessage");
		FormBean<Service> serviceBean = (FormBean<Service>) this.getFlash(
				request).get("serviceBean");
		map.put("service", serviceBean);
		return map;
	}

	public Map<String, Object> edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");
		if (id != null) {
			Service service = this.getServiceService().getService(
					Integer.parseInt(id));
			map.put("service", service);
		} else {
			FormBean<Service> serviceBean= (FormBean<Service>) this.getFlash(
					request).get("serviceBean");
			map.put("bean", serviceBean);
			map.put("service", serviceBean.getBean());
		}
		return map;
	}

	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = (String) request.getParameter("id");
		String ctp = request.getParameter("contextPath");
		Service service = this.getServiceService().getService(
				Integer.parseInt(id));
		FormBean<Service> bean = this.webDataBinder.bind(service, request);

		if (bean.getHasError()) {
			this.getFlash(request).put("serviceBean", bean);
			this.getFlash(request)
					.put("failureMessage", "message.saved.failed");
			return this.render("js_update_failure", map);
		}

		this.getFlash(request).put("successMessage", "message.saved.success");
		this.serviceService.update(bean.getBean());
		map.put("id", bean.getBean().getId());
		return this.render("js_update_success", map);
	}
}